﻿// ===============================================================================
// Copyright (c) 2007-2020 西安交通信息投资营运有限公司 版权所有
// 作者:张谦
// ===============================================================================

using System;
using XCI.Core;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows;

namespace XCI.Sys.Forms
{
    /// <summary>
    /// 系统在线用户管理
    /// </summary>
    [FormCaption("系统在线用户管理")]
    public partial class FrmOnlineUserManager : BaseForm
    {
        private readonly bool allowLogoff = SysApp.Permit("sys.param.logoff");

        public FrmOnlineUserManager()
        {
            InitializeComponent();
            gridControl.ModelType = typeof(SysOnlineUser);
        }

        /// <summary>
        /// 页面加载事件
        /// </summary>
        private async void FrmOnlineUserManager_Load(object sender, EventArgs e)
        {
            await gridControl.LoadAsync();
        }

        private async void BtnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            await gridControl.DeleteAsync();
        }

        private async void BtnSearch_Click(object sender, EventArgs e)
        {
            await gridControl.LoadAsync();
            gridView.ApplyFindFilter(editKey.Text.Trim());
        }

        private void GridControl_LoadData(object sender, LoadDataEventArgs e)
        {
            e.Task = async () =>
                new PagingList<SysOnlineUser>(await SysAccountService.Instance.GetOnlineUserListAsync(null));
        }

        private async void GridControl_DeleteData(object sender, DeleteDataEventArgs e)
        {
            await SysAccountService.Instance.LogoffOnlineUserByUserIdAsync(gridControl.SelectedIds);
            btnSearch.PerformClick();
        }

        private void GridControl_RowSelectionChanged(object sender, EventArgs e)
        {
            btnDelete.Enabled = gridControl.HasSingleSelected && allowLogoff;
        }
    }
}